
#region CreatTerm Function
function CreateTerm( $parent, $name, $url )  {
    if ($VerbosePreference -eq "Continue"){
        Write-Host -ForegroundColor Yellow "Adding term $name to parent $parent.Name"  
    }
   $term = $parent.CreateTerm("$name", 1033)  
  $term.IsAvailableForTagging = $true    
  $newUrl = $url
  #allow no url
  (![string]::IsNullOrEmpty($url))
  {
      if(!$url.ToLower().Contains("http"))
      {  
      # Logic to append site url to relative path to create complete url which is needed for Simple Link Navigation
        $newUrl = $appendUrl + $url
      }
     
  }
  $term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $newUrl)
  return $term
 } 
 #endregion
 
#region Function Documentation
<#
.Synopsis
   Short description
.DESCRIPTION
   Long description
.EXAMPLE
   Create-MetadataMenu -CSVFile "C:\tmp\publicmenu.csv" -appendUrl "http://intranet.contoso.com/sites/spbranding" -termGroupName "Site Collection - intranet.contoso.com-sites-spbranding" -termSetName "Custom Navigation Termset"

   CSV Format
   "L1Title" [0],"L1Url" [1],"L1NewWindow"[2],"L2Title"[3],"L2Url"[4],"L2NewWindow"[5],"L3Title"[6],"L3Url"[7],"L3NewWindow"[8]
   Additional Fields to be added dynamically to the array:
   L1Guid[9],L2Guid[10],L3Guid[11]
#>
#endregion
function Create-MetadataMenu
{
    [CmdletBinding()]
    [OutputType([int])]
#region Parameters
    Param
    (
        # Param1 
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $CSVFile,
        
        # Param2 
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        $appendUrl,

        # Param3 
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=2)]
        $termGroupName,

        # Param4 
        [Parameter(
                   ValueFromPipelineByPropertyName=$true,
                  Position=3)]
        $termSetName = "Managed Metadata Service",

        # Param5 
        [Parameter(
                   ValueFromPipelineByPropertyName=$true,
                   Position=4)]
        [string]$defaultMetadataServiceProxy = "Managed Metadata Service",
        # Param6 
        [Parameter(
                   ValueFromPipelineByPropertyName=$true,
                   Position=5)]
        [switch]$AlterUrlforNewWindow

  #endregion
    )

    Begin
    {
    }
    Process
    {
    cls
        
        #region Setting Up Termstore, Group, and Termset

            # Create termgroup and termset for My Site #get central admin
 
            $adminwebapp = Get-SPWebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}
            $caSite=Get-SPSite $adminwebapp.Url

            #get the Taxonomy session
            $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($caSite)

            #get the Term Store
            $termstore = $session.TermStores | where {$_.Name -eq $defaultMetadataServiceProxy}

            #create the group (if required)
            $group = $termstore.Groups["$termGroupName"]
            if ($group -eq $null)
            {
             $group = = $termstore.CreateGroup("$termGroupName");
             $group.Description = "Term Group description"
             $termStore.CommitAll()
             if ($VerbosePreference -eq "Continue"){
                Write-Host -ForegroundColor Yellow "TermGroup - $termGroupName created successfully"  
                }
            }


            #delete the term set (if it exists)
            try
            {
                if ($group.TermSets["$termSetName"] -ne $null) {  
                $group.TermSets["$termSetName"].Delete();
                #$group.TermSets | % { $_.Delete() } #Should delete all Termsets 
                 $termStore.CommitAll()
                 if ($VerbosePreference -eq "Continue"){
                    Write-Host -ForegroundColor Yellow "TermGroup - $termSetName Deleted or Absent"    
                  }
                }

            }
            catch 
            {
                Write-Host -ForegroundColor Yellow "Deletion of Termset Failed--Try deleting Termset from GUI"
                return
            }
        
        
        

 
            $termSet = $group.CreateTermSet($termSetName,1033)
            $termSet.Description = "Navigation TermSet description"
            $termSet.IsAvailableForTagging = $false
            $termSet.IsOpenForTermCreation = $false
            $navigationSet = $group.TermSets[$termSetName]
            $navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
            $termStore.CommitAll()
             if ($VerbosePreference -eq "Continue"){
                Write-Host -ForegroundColor Yellow "TermSet - $termSetName created successfully"  
                }
            #endregion
        
        #region ReadIn Termset

            $menuTerms=@()
             import-csv $CSVFile |
                          % { $menuTerms+=(,($_.L1Title, $_.L1Url, $_.L1NewWindow, $_.L2Title, $_.L2Url, $_.L2NewWindow, $_.L3Title, $_.L3Url, $_.L3NewWindow, $_.L1Guid, $_.L2Guid, $_.L3Guid) )}
        
        #endregion
        
        #region Build Arrays $L1Arr, $L1ArrUnique, $L2Arr, $L2ArrUnique, $L3Arr, $L3ArrUnique

                $L1Arr=@()
                $L1ArrUnique=@()
                for ($i=0; $i -lt $menuTerms.Count; $i++){
                        #using L1Title - $menuTerms[$i][0
                        $L1Arr += $menuTerms[$i][0]

                    }
                $L1ArrUnique=$L1Arr | Get-Unique -AsString
               if ($VerbosePreference -eq "Continue")
               {
                    Write-Host -ForegroundColor Yellow    $L1ArrUnique
                    for ($i=0; $i -lt $L1ArrUnique.Count; $i++){
                        Write-Host -ForegroundColor Yellow   $L1ArrUnique[$i] 
                    }
                }
                $L2Arr=@()
                $L2ArrUnique=@()
                for ($i=0; $i -lt $menuTerms.Count; $i++){
                    #L2Title - $menuTerms[$i][3]- not blank or null
                    if (![string]::IsNullOrEmpty($menuTerms[$i][3]))
                    {
                        $L2Arr+=(,($menuTerms[$i][0],$menuTerms[$i][3]))

                    }
                    }
                $L2ArrUnique=$L2Arr | Get-Unique -AsString
                if ($VerbosePreference -eq "Continue")
                {
                    Write-host -ForegroundColor Yellow $L2ArrUnique
                    for ($i=0; $i -lt $L2ArrUnique.Count; $i++){
                        Write-host -ForegroundColor Yellow $L2ArrUnique[$i][0] + " " +  $L2ArrUnique[$i][1]
                    }
                }

                $L3Arr=@()
                $L3ArrUnique=@()
                for ($i=0; $i -lt $menuTerms.Count; $i++){
                    #L2Title - $menuTerms[$i][3]- not blank or null and
                    #L3Title- $menuTerms[$i][6] - not blank or null
                    if ((![string]::IsNullOrEmpty($menuTerms[$i][3])) -and (![string]::IsNullOrEmpty($menuTerms[$i][6])))
                    {
                        $L3Arr+=(,($menuTerms[$i][0],$menuTerms[$i][3],$menuTerms[$i][6] ))

                    }
                    }
                $L3ArrUnique=$L3Arr | Get-Unique -AsString

                if ($VerbosePreference -eq "Continue")
                {
                    Write-host -ForegroundColor Yellow $L3ArrUnique
                    for ($i=0; $i -lt $L3ArrUnique.Count; $i++){
                        Write-host -ForegroundColor Yellow $L3ArrUnique[$i][0] + " => " +  $L3ArrUnique[$i][1]  + " => " +  $L3ArrUnique[$i][2]
                }

                }
        #endregion


    #region FirstLevel
        #Logic for L1
        #Go thru each unique term in the $L1ArrUnique 
            # foreach unique term find the first row where the term appears in the  $menuTerms
                #create the term
                #add the $tempTerm to the sort
                #exit the do while loop
         #After the big loop
            #set the sort
            #commit all
        $holdSort=$null
        for ($L1=0; $L1 -lt $L1ArrUnique.Count; $L1++)
        {
            $L1Value=$L1ArrUnique[$L1]
 
            $i=0
            do {
                $L1_0=$menuTerms[$i][0]
                if ($L1_0 -eq $L1Value){
                    #Create the Term
                    #L1Title - $menuTerms[$i][0] 
                    #L1Url - $menuTerms[$i][1]
                    #L1NewWindow - $menuTerms[$i][2]
                    if ( $AlterUrlforNewWindow -and  $menuTerms[$i][2] -eq 1 ){
                        $holdTempUrl=$menuTerms[$i][1] + "/NWND.aspx"
                        $tempTerm=CreateTerm $termSet $menuTerms[$i][0] $holdTempUrl
                    }
                    else #Not set to Open in New Window
                    {
                        $tempTerm=CreateTerm $termSet $menuTerms[$i][0] $menuTerms[$i][1]
                    
                    }

                    $holdSort+=$tempTerm.Id.toString() + ":"
                    }
                $i++
                } until ($L1_0 -eq $L1Value)

                #Update Guid for all entries
                for ($j=0; $j -lt $menuTerms.Count; $j++){
                    if ($menuTerms[$j][0] -eq $L1Value){
                        #L1Guid - $menuTerms[$j][9]

                        $menuTerms[$j][9]=$tempTerm.Id.toString()
                    }
                }
        
		        #get next $menuTerms
    
       
        }

        $holdSort=$holdSort.TrimEnd(':')
        $termSet.CustomSortOrder=$holdSort
        $termStore.CommitAll()

        if ($VerbosePreference -eq "Continue")
        {
         #Check Titles, Urls,Guids for Level 1
         for ($j=0; $j -lt $menuTerms.Count; $j++){
                         #L1Title - $menuTerms[$j][0]
                         #L1Url - $menuTerms[$j][1]
                         #L1Guid - $menuTerms[$j][9]
                         Write-host -ForegroundColor Yellow ($menuTerms[$j][0] + " ==> " + $menuTerms[$j][1] + " Guid ==> " + $menuTerms[$j][9])
                    }
        }
         #Build $L1UnqPlus
         #$L1UnqPlus does not contain elements with an empty Second Position (StandAlone Level 1s)
         $L1UnqPlusGuid=@()

         for ($i=0; $i -lt $L1ArrUnique.Count; $i++){
            $Current = $L1ArrUnique[$i]
                $j=0
                do {
                    $MenuCurrent=$menuTerms[$j][0]
                    if ($MenuCurrent -eq $Current){
                        #check if there is a level2
                        #L1Title - $menuTerms[$j][3] 
                        if (![string]::IsNullOrEmpty($menuTerms[$j][3])){
                            #Not non-trivial syntax for adding 2 array elements at a time
                            #L1Title - $menuTerms[$j][0]
                            #L1Guid - $menuTerms[$j][9]
                            $L1UnqPlusGuid +=(,($menuTerms[$j][0],$menuTerms[$j][9]))
                            }
                        }
                        $j++

                } until ($MenuCurrent -eq $Current)
        
            }

         #Check Label and Guids for Level 1
         #Will Not Show Up if no Second-Level
         if ($VerbosePreference -eq "Continue")
        {
         for ($j=0; $j -lt $L1UnqPlusGuid.Count; $j++){
                        #L1Guid - $menuTerms[$j][9]
                        Write-host -ForegroundColor Yellow $L1UnqPlusGuid[$j][0] + " ==> " + $L1UnqPlusGuid[$j][1]
    
             }
         }
    #endregion
    #region SecondLevel
         #Second Level Logic
         #First go thru  $L1UnqPlusGuid
            #Get Variables $L1PlusLabel, $L1PlusGuid
                #Go thru the TermSet and find the node matching $L1PlusGuid
                    #For every 2nd Level Term L2ArrUnique that matches the $L1PlusLabel
                        # get two variables 
                            #$L2Unq_0=$L2ArrUnique[$n][0]
                            #$L2Unq_1=$L2ArrUnique[$n][1]
                        #Do Loop -Stop at First Match of both variables
                            #Create the NewTerm with $Term  [1]-(2nd Level Label) and [3]-(URL Element)
                        #End Do
                            #Set The [5]-(L2 Guid) Variable Every Match of the Two Terms [0]-(1st Level) [1]-(2nd Level) to the write $L2Guid into the $menuTerms row
                    #Finish Sort on $Term with Guid
                 #Continue For every 2nd Level Term L2ArrUnique that matches the $L1PlusLabel

         <# "L1Title" [0],"L1Url" [1],"L1NewWindow"[2],"L2Title"[3],"L2Url"[4],"L2NewWindow"[5],"L3Title"[6],"L3Url"[7],"L3NewWindow"[8]
           Additional Fields to be added dynamically to the array:
           L1Guid[9],L2Guid[10],L3Guid[11]
        #>       

            for ($L1=0; $L1 -lt $L1UnqPlusGuid.Count; $L1++){
                    $L1PlusLabel = $L1UnqPlusGuid[$L1][0]
                    $L1PlusGuid = $L1UnqPlusGuid[$L1][1]
                    foreach($term in $termSet.GetAllTerms())  {
                        $holdSort=$null
                        if ($term.Id -eq $L1PlusGuid){ 
                                #Loop thru $L2ArrUnique for all matches of $L1PlusLable
                                for($L2=0; $L2 -lt $L2ArrUnique.Count; $L2++){
                                        if ($L2ArrUnique[$L2][0] -eq $L1PlusLabel){
                                    
                                            $L2Unq_0=$L2ArrUnique[$L2][0]
                                            $L2Unq_1=$L2ArrUnique[$L2][1]

                                            #Now find first match in $menuTerms
                                            $i=0
                                            do 
                                            {
                                            #L1Title - $menuTerms[$j][0]
                                            #L2Title - $menuTerms[$j][3]

                                                $L2_0=$menuTerms[$i][0]
                                                $L2_1=$menuTerms[$i][3]
                                                if ($L2_0 -eq $L2Unq_0 -and $L2_1 -eq $L2Unq_1 ){
                                                    #Create the Second-Level Term
                                                     #L1Title - $menuTerms[$i][0]
                                                     #L2Title - $menuTerms[$i][3]
                                                     #L2Url -   $menuTerms[$i][4]
                                                     #L2NewWindow - $menuTerms[$i][5]
                                                    if ( $AlterUrlforNewWindow -and  $menuTerms[$i][5] -eq 1 ){
                                                        $holdTempUrl=$menuTerms[$i][4] + "/NWND.aspx"
                                                        $tempTerm=CreateTerm $term $menuTerms[$i][3] $holdTempUrl
                                                    }
                                                    else #Not set to Open in New Window
                                                    {
                                                            $tempTerm=CreateTerm $term $menuTerms[$i][3] $menuTerms[$i][4]
                    
                                                    }

                                                   
                                                    $holdSort+=$tempTerm.Id.toString() + ":"
                                                    }
                                                $i++
                                           } until ($L2_0 -eq $L2Unq_0 -and $L2_1 -eq $L2Unq_1 )
                                           $holdL2Guid=$tempTerm.Id.ToString()

                                            #Update Guid for all entries
                                            for ($j=0; $j -lt $menuTerms.Count; $j++){
                                                 #L1Title - $menuTerms[$j][0]
                                                 #L2Title - $menuTerms[$j][3]
                                                 $L2_0=$menuTerms[$j][0]
                                                 $L2_1=$menuTerms[$j][3]
                                                if ($L2_0 -eq $L2Unq_0 -and $L2_1 -eq $L2Unq_1 ){
                
                                                        #L2Guid - $menuTerms[$j][10]
                                                        $menuTerms[$j][10]=$holdL2Guid

                                                   } #End if ($L2_0 -eq $L2Unq_0 -and $L2_1 -eq $L2Unq_1 )
									        } #End for ($j=0; $j -lt $menuTerms.Count; $j++)
                                       } #End  if ($L2ArrUnique[$L2][0] -eq $L1PlusLabel)
                                } # Loop thru $L2ArrUnique
					
					            #Finished Looping Throug L2_terms
                                if ($holdSort -ne $null){
                                    $holdSort=$holdSort.TrimEnd(':')
                                    $term.CustomSortOrder=$holdSort
						            }
					            $termStore.CommitAll()
                            } #if ($term.Id -eq $L1PlusGuid)
                 } # foreach keep going thru termset   
               
    
             } #End for ($L1=0; $L1 -lt $L1UnqPlusGuid.Count; $L1++)

                #Build $L2UnqPlus
                 #$L2UnqPlus does not contain elements with an empty Third Position [2] (StandAlone Level 2s)
                 $L2UnqPlusGuid=@()

                 for ($i=0; $i -lt $L2ArrUnique.Count; $i++){
                    $Current_0 = $L2ArrUnique[$i][0]
                    $Current_1 = $L2ArrUnique[$i][1]
                        $j=0
                        #Only get 1 Item for 2 Levels
                        do {

                            #L1Title - $menuTerms[$j][0]
                            #L2Title - $menuTerms[$j][3]
                            $MenuCurrent_0=$menuTerms[$j][0]
                            $MenuCurrent_1=$menuTerms[$j][3]
                            if ($MenuCurrent_0 -eq $Current_0 -and $MenuCurrent_1 -eq $Current_1){
                                #check if there is a level3 Title
                                 #L3Title - $menuTerms[$j][6]
                                if (![string]::IsNullOrEmpty($menuTerms[$j][6])){
                                    #Not non-trivial syntax for adding 3 array elements at a time
                                    #
                                    #L1Title - $menuTerms[$j][0]
                                    #L2Title - $menuTerms[$j][3]
                                    #L2Guid - $menuTerms[$j][10]
                                    $L2UnqPlusGuid +=(,($menuTerms[$j][0],$menuTerms[$j][3],$menuTerms[$j][10]))
                                    }
                                }
                                $j++

                        } until ($MenuCurrent_1 -eq $Current_1 -and $MenuCurrent_2 -eq $Current_2)
        
                    }



        #Check Titles, Urls,Guids for Level 1
         #for ($j=0; $j -lt $menuTerms.Count; $j++){
         #                #L1Title - $menuTerms[$j][0]
         #                #L1Url - $menuTerms[$j][1]
         #                #L2Title - $menuTerms[$j][3]
         #                #L2Url - $menuTerms[$j][4]
         #                #L1Guid - $menuTerms[$j][9]
         #                #L2Guid - $menuTerms[$j][10]

         #                $menuTerms[$j][0] + " ==> " + $menuTerms[$j][1] + " Guid ==> " + $menuTerms[$j][9] + " " + $menuTerms[$j][3] + " ==> " + $menuTerms[$j][4] + " Guid ==> " + $menuTerms[$j][10]
         #           }

         #Will Not Show up if no 3rd Level
         #Check Label and Guids for Level 2
         #for ($j=0; $j -lt $L2UnqPlusGuid.Count; $j++){
         #              $L2UnqPlusGuid[$j][0] + " ==> " + $L2UnqPlusGuid[$j][1]+ " Guid:==>" + $L2UnqPlusGuid[$j][2]
         #  }

        #endregion
	#region Third Level Logic
        #Third Level Logic
         #First go thru  $L1UnqPlusGuid
            #Get Variables $L1PlusLabel, $L1PlusGuid
                #Go thru the TermSet and find the node matching $L1PlusGuid
                    #For every 2nd Level Term L2ArrUnique that matches the $L1PlusLabel
                        # get two variables 
                            #$L2Unq_0=$L2ArrUnique[$n][0]
                            #$L2Unq_1=$L2ArrUnique[$n][1]
                        #Do Loop -Stop at First Match of both variables
                            #Create the NewTerm with $Term  [1]-(2nd Level Label) and [3]-(URL Element)
                        #End Do
                            #Set The [5]-(L2 Guid) Variable Every Match of the Two Terms [0]-(1st Level) [1]-(2nd Level) to the write $L2Guid into the $menuTerms row
                    #Finish Sort on $Term with Guid
                 #Continue For every 2nd Level Term L2ArrUnique that matches the $L1PlusLabel
        <#
           "L1Title" [0],"L1Url" [1],"L1NewWindow"[2],"L2Title"[3],"L2Url"[4],"L2NewWindow"[5],"L3Title"[6],"L3Url"[7],"L3NewWindow"[8]
           Additional Fields to be added dynamically to the array:
           L1Guid[9],L2Guid[10],L3Guid[11]
        #>

		 for ($L2=0; $L2 -lt $L2UnqPlusGuid.Count; $L2++){
					$L2PlusLabel_0 = $L2UnqPlusGuid[$L2][0]
					$L2PlusLabel_1 = $L2UnqPlusGuid[$L2][1]
					$L2PlusGuid = $L2UnqPlusGuid[$L2][2]
					$termsetCount=0
					foreach($term in $termSet.GetAllTerms())  {
						
						$holdSort=$null
						if ($term.Id -eq $L2PlusGuid){ 
								#Loop thru $L3ArrUnique for all matches of $L2PlusLable
								for($L3=0; $L3 -lt $L3ArrUnique.Count; $L3++){
										if ($L3ArrUnique[$L3][0] -eq $L2PlusLabel_0 -and $L3ArrUnique[$L3][1] -eq $L2PlusLabel_1){
											$L3Unq_0=$L3ArrUnique[$L3][0]
											$L3Unq_1=$L3ArrUnique[$L3][1]
											$L3Unq_2=$L3ArrUnique[$L3][2]
											#Now find first match in $menuTerms
											$i=0
											do 
											{
												#L1Title - $menuTerms[$j][0]
												#L2Title - $menuTerms[$j][3]
												#L2Title - $menuTerms[$j][6]
											   


												$L3_0=$menuTerms[$i][0]
												$L3_1=$menuTerms[$i][3]
												$L3_2=$menuTerms[$i][6]

												if ($L3_0 -eq $L3Unq_0 -and $L3_1 -eq $L3Unq_1 -and  $L3_2 -eq $L3Unq_2){
													#Create the Term
													#Third Term [2]
													#URL [3]
													#L3Title - $menuTerms[$j][6]
													#L3Url -   $menuTerms[$j][7]
                                                    #L2NewWindow - $menuTerms[$i][8]
                                                    if ( $AlterUrlforNewWindow -and  $menuTerms[$i][8] -eq 1 ){
                                                        $holdTempUrl=$menuTerms[$i][7] + "/NWND.aspx"
                                                        $tempTerm=CreateTerm $term $menuTerms[$i][6] $holdTempUrl
                                                    }
                                                    else #Not set to Open in New Window
                                                    {
													    $tempTerm=CreateTerm $term $menuTerms[$i][6] $menuTerms[$i][7]
                    
                                                    }
													$holdSort+=$tempTerm.Id.toString() + ":"
													}
												$i++
										   } until ($L3_0 -eq $L3Unq_0 -and $L3_1 -eq $L3Unq_1 -and  $L3_2 -eq $L3Unq_2)
										   $holdL3Guid=$tempTerm.Id.ToString()

											#Update Guid for all entries
											for ($j=0; $j -lt $menuTerms.Count; $j++){

												#L1Title - $menuTerms[$j][0]
												#L2Title - $menuTerms[$j][3]
												#L3Title - $menuTerms[$j][6]
												 $L3_0=$menuTerms[$j][0]
												 $L3_1=$menuTerms[$j][3]
												 $L3_2=$menuTerms[$j][6]
												if ($L3_0 -eq $L3Unq_0 -and $L3_1 -eq $L3Unq_1 -and  $L3_2 -eq $L3Unq_2 ){
						
														#Guid LeveL3 - $menuTerms[$j][11]
														$menuTerms[$j][11]=$holdL3Guid
												   } #End if ($L3_0 -eq $L3Unq_0 -and $L3_1 -eq $L3Unq_1 -and  $L3_2 -eq $L3Unq_2)
											} #End for ($j=0; $j -lt $menuTerms.Count; $j++)
									   } #End if ($L3ArrUnique[$L3][0] -eq $L2PlusLabel_0 -and $L3ArrUnique[$L3][1] -eq $L2PlusLabel_1)
								} # Loop thru $L3ArrUnique
							
								#Finished Looping Throug L3_terms
								if ($holdSort -ne $null){
									$holdSort=$holdSort.TrimEnd(':')
									$term.CustomSortOrder=$holdSort
									}
								$termStore.CommitAll()
							} #if ($term.Id -eq $L2PlusGuid)
							$TermsetCount++
				 } # foreach keep going thru termset   
					   
			
			 } #End for ($L2=0; $L2 -lt $L2UnqPlusGuid.Count; $L2++)
			#endregion
    }
    End
    {
        $session=$null
        $caSite.Dispose();

    }
}

$VerbosePreference="Continue"
Create-MetadataMenu -CSVFile "C:\tmp\publicmenu.csv" -appendUrl "http://intranet.contoso.com/sites/spbranding" -termGroupName "MetadataNav - intranet.contoso.com-sites-spbranding" -termSetName "SPBranding" -AlterUrlforNewWindow
